#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tornado
import tornado.web
try:
    import ujson as json
except:
    import json
from controls import mongo
from controls import data_message_system_content_mysql

class BaseHandler(tornado.web.RequestHandler):
    def get_current_user(self):
        return self.get_secure_cookie("username")

class AddSystemMessageHandler(BaseHandler):
    def post(self, *args, **kwargs):
        content = self.get_argument('content', None)

        if content is None:
            res = {'ret':1, 'msg':'content is none', 'res':{}}
            self.write(json.dumps(res))
            return

        messageid = mongo.count_messageid()

        item = {
            'messageid': messageid,
            'content': content
        }

        if data_message_system_content_mysql.add_system_message_content_item(item):
            res = {'ret':0, 'msg':'system message add successful', 'res':{}}
            self.write(json.dumps(res))
            return
        else:
            res = {'ret':1, 'msg':'system message add faild', 'res':{}}
            self.write(json.dumps(res))
            return